{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "440d361e-f41e-494a-98a3-aa87cf0087af",
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-12-18T20:21:38.842996Z",
     "end_time": "2023-12-18T20:21:39.189126Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "56c9c6a0-5ed8-4708-b96f-19ea7f50f5b9",
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-12-18T20:21:39.194637Z",
     "end_time": "2023-12-18T20:21:39.216736Z"
    }
   },
   "outputs": [],
   "source": [
    "# 创建一个空白数据框\n",
    "df = pd.DataFrame()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "152209ca-abfb-436f-98c1-69f762515cc2",
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-12-18T20:21:39.216736Z",
     "end_time": "2023-12-18T20:21:39.239554Z"
    }
   },
   "outputs": [],
   "source": [
    "# 增加两行一列\n",
    "df['编号'] = [1, 2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c0398b1a-3b0d-4cc6-b1a8-a1f58ad8a5de",
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-12-18T20:21:39.239554Z",
     "end_time": "2023-12-18T20:21:39.255524Z"
    }
   },
   "outputs": [],
   "source": [
    "# 增加一列\n",
    "df.insert(1, '牌面', ['A', 2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "7274fac7-b74d-4b39-ba88-d10831db3eee",
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-12-18T20:21:39.255524Z",
     "end_time": "2023-12-18T20:21:39.272923Z"
    }
   },
   "outputs": [],
   "source": [
    "# 中间再增加一列\n",
    "df.insert(1, '花色', ['黑桃♠', '红心♥'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "656a751f-77a2-435f-a0c0-88d1bb248597",
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-12-18T20:23:22.658096Z",
     "end_time": "2023-12-18T20:23:22.674425Z"
    }
   },
   "outputs": [],
   "source": [
    "# 定义一张新牌\n",
    "poker1 = pd.DataFrame({'花色': ['红心♥'], '牌面': [3]})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "c0c5d306-2f66-47dd-91c0-dc5a9adba76d",
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-12-18T20:26:37.696411Z",
     "end_time": "2023-12-18T20:26:37.707272Z"
    }
   },
   "outputs": [],
   "source": [
    "# 增加一行\n",
    "df2 = pd.concat([df, poker1], ignore_index=True, sort=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "data": {
      "text/plain": "    编号   花色 牌面\n1  2.0  红心♥  2",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>编号</th>\n      <th>花色</th>\n      <th>牌面</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1</th>\n      <td>2.0</td>\n      <td>红心♥</td>\n      <td>2</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 删除索引为0和2的行\n",
    "df2.drop([0, 2])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-12-18T20:27:28.219764Z",
     "end_time": "2023-12-18T20:27:28.235985Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [
    {
     "data": {
      "text/plain": "    编号 牌面\n0  1.0  A\n1  2.0  2\n2  NaN  3",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>编号</th>\n      <th>牌面</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1.0</td>\n      <td>A</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>2.0</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>NaN</td>\n      <td>3</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 删除“花色”列\n",
    "df2.drop(['花色'], axis=1)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-12-18T20:28:46.246155Z",
     "end_time": "2023-12-18T20:28:46.294487Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [],
   "source": [
    "# 删除“编号”列，用del会改变原来的数据\n",
    "del df2['编号']"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-12-18T20:30:09.061729Z",
     "end_time": "2023-12-18T20:30:09.129644Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [],
   "source": [
    "# 将所有的A修改为1\n",
    "df3 = df2.replace('A', 1)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-12-18T20:30:50.155112Z",
     "end_time": "2023-12-18T20:30:50.171245Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "outputs": [
    {
     "data": {
      "text/plain": "   花色  牌面\n0  黑桃   1\n1  红心   2\n2  红心   3",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>花色</th>\n      <th>牌面</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>黑桃</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>红心</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>红心</td>\n      <td>3</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将所有的符号剔除\n",
    "df3.replace(['♠', '♥', '♣', '♦'], '', regex=True)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-12-18T20:33:32.244660Z",
     "end_time": "2023-12-18T20:33:32.276610Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "outputs": [
    {
     "data": {
      "text/plain": "    花色  牌面\n0  黑桃♠   1\n1  红心♥   2\n2  红心♥   3",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>花色</th>\n      <th>牌面</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>黑桃♠</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>红心♥</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>红心♥</td>\n      <td>3</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df3"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-12-18T20:33:56.465443Z",
     "end_time": "2023-12-18T20:33:56.500892Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "outputs": [],
   "source": [
    "# 修改某一个值\n",
    "df3.loc[0, '牌面'] = 9"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-12-18T20:36:34.705487Z",
     "end_time": "2023-12-18T20:36:34.713954Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "outputs": [],
   "source": [
    "df3.columns = list('AB')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-12-18T20:37:08.449789Z",
     "end_time": "2023-12-18T20:37:08.468764Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "outputs": [
    {
     "data": {
      "text/plain": "    花色  B\n0  黑桃♠  9\n1  红心♥  2\n2  红心♥  3",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>花色</th>\n      <th>B</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>黑桃♠</td>\n      <td>9</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>红心♥</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>红心♥</td>\n      <td>3</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df3.rename({'A': '花色'}, axis=1)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-12-18T20:37:52.861475Z",
     "end_time": "2023-12-18T20:37:52.905132Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "outputs": [
    {
     "data": {
      "text/plain": "     A  B\n0  黑桃♠  9\n3  红心♥  2\n3  红心♥  3",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>A</th>\n      <th>B</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>黑桃♠</td>\n      <td>9</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>红心♥</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>红心♥</td>\n      <td>3</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 修改指定行索引\n",
    "df3.rename({1: 3, 2: 3})"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-12-18T20:38:50.394590Z",
     "end_time": "2023-12-18T20:38:50.410386Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
